<?php
/**
* @file Verifiedm.php
* @synopsis  执业医师资格提交
* @author Yee, <rlk002@gmail.com>
* @version 1.0
* @date 2015-03-31 16:52:13
*/


class Verifiedm extends CI_Model
{
	function __construct()
	{
		
	}

	public function insert_verified_log($verified_info)
	{
		$result = $this->db->insert("ml_verified_log",$verified_info);
		if($result)
		{
			$return = $this->db->insert_id();
			if($return)
			{
				$this->db->set("verified_status",2)->where("id",$verified_info['user_id'])->update("ml_fellow");
				$mc = $this->cache->memcached;
		        if(!$this->cache->memcached->is_supported())
		        {
		            $result = array("code" => -3,'tips' => "缓存未开启");
		        }
		        //删除缓存
		        $key = 'getUserInfoALLById_' . $verified_info['user_id'];
    			$mc->delete($key);
			}
			else
			{
				return false;
			}
		}
		else
		{
			$return = false;
		}
		return $return;
	}
	    //取相关科认证用户
    public function verified_userlist_of_thread($threads,$oneThreadSize=5)
    {
        $verifiedusers = array();
        $mc = $this->cache->memcached;
        if(!$this->cache->memcached->is_supported())
        {
            $result = array("code" => -3,'tips' => "缓存未开启");
        }
        foreach($threads as $thread)
        {
            $mc_key = 'verified_userlist_of_thread__' . $thread;
            $result = $mc->get($mc_key);
            if(!$result)
            {
                $result = $this->db->select('ml_fellow.id,ml_fellow.username,ml_thread.thread_name as invite_thread,ml_fellow.head_ico,ml_fellow.expert_info,ml_fellow.user_level,ml_fellow.practice_hospital,ml_fellow.post_title,ml_fellow.truename')->from('ml_fellow')->join("ml_thread","ml_thread.id=ml_fellow.thread","left")->where(array('ml_fellow.verified_status'=>3, 'ml_fellow.thread'=>$thread))->order_by('ml_fellow.comment_count','desc')->limit(5,0)->get()->result_array();

                $i = 0;
                $cache_data = array();
				foreach($result as $row)
				{
					$row['head_ico'] = ret_fullpath_url($row['head_ico']);
                    if($row['user_level'] == 5)
                    {
                        $row['is_expert'] = 1;
                    }
                    else
                    {
                        $row['is_expert'] = 0;
                        $row['truename'] = '';
                    }

                    if(is_numeric($row['post_title']))
                    {
                        ci_load_once('posttitlem');
                        $row['post_title'] = $this->posttitlem->getPosttitleName($row['post_title']);
                    }
                    if($i < $oneThreadSize && $this->uid != $row['id']) {//如果客户端帖子没有分类，就返回所有科室专家列表，但不能像有分类时一样每科室返回太多，那样数据太大，所以加了$oneThreadSize参数控制，且缓存里依然存5个
                        $verifiedusers[] = $row;
                        $cache_data[] = $row;
                        $i++;
                    }
				}
                if($cache_data && count($cache_data) > 0)
                {
                    $mc->save($mc_key,$cache_data,3600);
                }
            }
			else
			{
                $result = array_slice($result,0,$oneThreadSize);
				$verifiedusers = array_merge($verifiedusers,$result);
			}
        }
        return $verifiedusers;
    }
    public function post_title_contrast()
    {
        $post_title_list = array(
            array("id"=>1,"post_title"=>"医学生"),
            array("id"=>2,"post_title"=>"实习医师"),
            array("id"=>3,"post_title"=>"住院医师"),
            array("id"=>4,"post_title"=>"住院总医师"),
            array("id"=>5,"post_title"=>"主治医师"),
            array("id"=>6,"post_title"=>"副主任医师"),
            array("id"=>7,"post_title"=>"主任医师"),
            array("id"=>8,"post_title"=>"药士"),
            array("id"=>9,"post_title"=>"药师"),
            array("id"=>10,"post_title"=>"主管药师"),
            array("id"=>11,"post_title"=>"副主任药师"),
            array("id"=>12,"post_title"=>"主任药师"),
            array("id"=>13,"post_title"=>"护士"),
            array("id"=>14,"post_title"=>"护师"),
            array("id"=>15,"post_title"=>"主管护师"),
            array("id"=>16,"post_title"=>"副主任护师"),
            array("id"=>17,"post_title"=>"主任护师"),
            array("id"=>18,"post_title"=>"技师"),
            array("id"=>19,"post_title"=>"营养师"),
            array("id"=>20,"post_title"=>"康复治疗师"),
            array("id"=>21,"post_title"=>"心理咨询师"),
            array("id"=>22,"post_title"=>"医务工作者"),
            array("id"=>23,"post_title"=>"其它")
        );
        return $post_title_list;
    }
    public function transform_id_post_title($value)
    {
        ci_load_once('posttitlem');
        return $this->posttitlem->getPosttitleName($value);
//        $post_title_list = $this->post_title_contrast();
//        if(is_numeric($value))//如果是数字，转成文字title
//        {
//            foreach($post_title_list as $item)
//            {
//                if($item['id'] == $value)
//                {
//                    return $item['post_title'];
//                }
//            }
//
//            return "其它";
//        }
//        return $value;
    }
    public function transform_post_title_id($value)
    {
        $post_title_list = $this->post_title_contrast();
        foreach($post_title_list as $item) {
            if ($item['post_title'] == $value) {
                return $item['id'];
            }
        }
        return $value;
    }

    //是否提交了正在等待审核
    public function is_verified_log_exists($uid)
    {
        $return =  $this->db->select("count(id) as count")->from('ml_verified_log')->where(array("user_id"=>$uid,"verified_status"=>2,"is_show"=>1))->get()->result_array();
        return $return[0]['count'];
    }

    /**
     * 判断重新审核,如果用户提交了新的认证信息,重新审核的信息自动失败
     * @param $user_id
     */
    public function update_again_verified($user_id)
    {
        $filter = array('user_id' => $user_id, 'verified_status' => 10, 'is_show' => 1);
        $info = $this->db->where($filter)->order_by('id', 'DESC')->limit(1)->get('ml_verified_log')->row_array();
        // 存在更新,添加一条认证记录
        if ($info) {
            $this->db->update('ml_verified_log', array('verified_status' => 4), array('id' => $info['id']));

            $record_data = array(
                'verified_log_id' => $info['id'],
                'user_id' => $info['user_id'],
                'verified_status' => 4,
                'remark' => '再次提交,重新审核自动审核失败',
            );
            $this->db->insert('ml_verified_record', $record_data);
        }
        return true;
    }

    public function update_verified_status_tofail($uid)
    {
        $this->db->set("verified_status",4);
        $this->db->where("user_id",$uid);
        return $this->db->update('ml_verified_log');
    }

    public function verified_status_tofail_for_modifyuserinfo($uid)
    {
        $filter = array('user_id' => $uid);
        $info = $this->db->where($filter)->where_in('verified_status', array(2,10))->limit(1)->get('ml_verified_log')->row_array();
        if ($info) {
            $this->db->update('ml_verified_log', array('verified_status' => 4), array('id' => $info['id']));
            $record_data = array(
                'verified_log_id' => $info['id'],
                'user_id' => $info['user_id'],
                'verified_status' => 4,
                'remark' => '修改信息,上次待审核记录自动审核失败',
            );
            return $this->db->insert('ml_verified_record', $record_data);
        }
        return true;
    }
}
?>
